package cn.ly.security.controller;

import cn.ly.security.entity.Equipment;
import cn.ly.security.service.impl.EquipmentServiceImpl;
import cn.ly.security.util.DataPojo;
import cn.ly.security.util.Pagely;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/Equipment")
@CrossOrigin("*")
@Api(tags="设备种类")
public class EquipmentController {
    @Autowired
    EquipmentServiceImpl equipmentService;
    @DeleteMapping("/deleteByPrimaryKey")
    public boolean deleteByPrimaryKey(Integer id) {
        boolean i = equipmentService.removeById(id);
        return i;
    }
    @PutMapping("/insert")
    public boolean insert(Equipment record) {
        boolean insert = equipmentService.save(record);
        return insert;
    }
    @GetMapping("/selectByPrimaryKey")
    public Equipment selectByPrimaryKey(Integer id) {
        Equipment equipment = equipmentService.getById(id);

        return equipment;
    }

    @PostMapping("/updateByPrimaryKey")
    public boolean updateByPrimaryKey(Equipment record) {
        boolean i = equipmentService.updateById(record);
        return i;
    }

    @GetMapping("/selectAll")
    public DataPojo selectAll(Pagely pagely) {
        Page<Equipment> userPage = new Page<>(pagely.getCurrent(), pagely.getSize());
        Page<Equipment> page = equipmentService.page(userPage);
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(page);
        return new DataPojo("200","无异常",objects);
    }

    @GetMapping("/selectByName")
    public DataPojo selectByName(@PathVariable("name") String name, Pagely pagely) {
        Page<Equipment> equipmentPage = new Page<>(pagely.getCurrent(), pagely.getSize());
        QueryWrapper<Equipment> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.like("name",name);
        Page<Equipment> page = equipmentService.page(equipmentPage, objectQueryWrapper);
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(page);
        return new DataPojo("200","无异常",objects);
    }
}
